<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JS-事件-案例</title>
</head>
<body>
<img id="light" src="img/off.gif"> <br>

<input type="button" value="点亮" onclick="on()">
<input type="button" value="熄灭" onclick="off()">

<br> <br>

<!--onfocus聚焦事件,onblur离焦事件-->
<input type="text" id="name" value="ITCAST" onfocus="lower()" onblur="upper()">
<br> <br>

<input type="checkbox" name="hobby"> 电影
<input type="checkbox" name="hobby"> 旅游
<input type="checkbox" name="hobby"> 游戏
<br>

<input type="button" value="全选" onclick="checkAll()">
<input type="button" value="反选" onclick="reverse()">

</body>

<script>

    //1. 点击 "点亮" 按钮, 点亮灯泡; 点击 "熄灭" 按钮, 熄灭灯泡; -- onclick
    function on() {
        var img = document.getElementById("light");
        img.src = "img/on.gif";
    }

    function off() {
        var img = document.getElementById("light");
        img.src = "img/off.gif";
    }

    //2. 输入框聚焦后, 展示小写; 输入框离焦后, 展示大写; -- onfocus , onblur

    function lower() {
        var text = document.getElementById("name");
        text.value = text.value.toLowerCase();
    }

    function upper() {
        var text = document.getElementById("name");
        text.value = text.value.toUpperCase();
    }

    //3. 点击 "全选" 按钮使所有的复选框呈现选中状态 ; 点击 "反选" 按钮使所有的复选框呈现取消勾选的状态 ; -- onclick
    function checkAll() {
        var ins = document.getElementsByName('hobby');
        for (var i = 0; i < ins.length; i++) {
            const e = ins[i];
            e.checked = true;
        }
    }

    function reverse() {
        var ins = document.getElementsByName('hobby');
        for (var i = 0; i < ins.length; i++) {
            const e = ins[i];
            e.checked = false;
        }
    }
</script>
</html>